package cn.fengzhilun.javase.day14.day10;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import org.junit.Test;

import cn.fengzhilun.javase.day14.day09.Score;

public class TestMap {
	@Test
	public void test1() {
		System.out.println(getWeekDayNum(7));
		Set<Entry<Integer,String>> entrySet = WEEK_DAY.entrySet();
		for(Entry entry : entrySet) {
			System.out.println("key:" + entry.getKey() + "value:" + entry.getValue());
		}
		Set<Integer> keyset = WEEK_DAY.keySet();
		for(Integer key : keyset) {
			System.out.println("key:" + key +"value :" + WEEK_DAY.get(key));
		}
	}
	
	public static String getWeekDayNum(int num) {
		//String[] week = {"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
		
		//return week[num-1];
//		Map<Integer,String> map = new HashMap<>();
//		map.put(1, "星期一");
//		map.put(2, "星期二");
//		map.put(7, "星期天");
		
		
		return WEEK_DAY.get(num);
	}
	static final Map<Integer,String> WEEK_DAY = new HashMap<>();
	static {
		WEEK_DAY.put(1, "星期一");
		WEEK_DAY.put(2, "星期二");
		WEEK_DAY.put(7, "星期天");
	}
	
	@Test
	public void test2() throws IOException {
		List<String> lines = Files.readAllLines(Paths.get("data/scores.csv"));
		Map<String, Integer> stat = new HashMap<>();
		for(String line : lines) {
			//System.out.println(line);
			String[] split = line.split("[, ]");
			for(String word : split) {
				if(stat.containsKey(word)) {
					stat.put(word, stat.get(word) + 1);
				}else{
					stat.put(word, 1);
				}
				
			}
		}
		System.out.println(stat);	
	}
	
	@Test
	public void test3() throws IOException {
		List<String> lines = Files.readAllLines(Paths.get("data/scores.csv"));
		
		Map<String,List<Score>> map = new HashMap<>();
		
		for(String line : lines) {
			String[] split = line.split("[ ,]");
			String name = split[0];
			String course = split[1];
			double score = Double.parseDouble(split[2]);
			
			if(map.containsKey(name)) {
				List<Score> list = map.get(name);
				list.add(new Score(score, name, course));
			}else {
				List<Score> list = new ArrayList<>();
				list.add(new Score(score, name, course));
				map.put(name, list);
				
			}
		}
	}
	@Test
	public void test4() {
		List<Integer> list = new ArrayList<>();
		
		for (int i = 1; i <= 10; i++) {
			list.add(i);
		}
		
		while(list.size() > 0) {
			int index = (int)(Math.random()*list.size());
			System.out.println(list.get(index));
			list.remove(index);
		}
		
	}
}
